package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.ProjectInfo;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.ProjectInfoRepository;
import co.yixiang.modules.shop.service.ProjectInfoService;
import co.yixiang.modules.shop.service.dto.ProjectInfoDto;
import co.yixiang.modules.shop.service.dto.ProjectInfoQueryCriteria;
import co.yixiang.modules.shop.service.mapper.ProjectInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "projectInfo")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectInfoServiceImpl implements ProjectInfoService {

    private final ProjectInfoRepository projectInfoRepository;

    private final ProjectInfoMapper projectInfoMapper;

    public ProjectInfoServiceImpl(ProjectInfoRepository projectInfoRepository, ProjectInfoMapper projectInfoMapper) {
        this.projectInfoRepository = projectInfoRepository;
        this.projectInfoMapper = projectInfoMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectInfoQueryCriteria criteria, Pageable pageable){
        Page<ProjectInfo> page = projectInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectInfoMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectInfoDto> queryAll(ProjectInfoQueryCriteria criteria){
        return projectInfoMapper.toDto(projectInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectInfoDto findById(Integer id) {
        ProjectInfo projectInfo = projectInfoRepository.findById(id).orElseGet(ProjectInfo::new);
        ValidationUtil.isNull(projectInfo.getId(),"ProjectInfo","id",id);
        return projectInfoMapper.toDto(projectInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectInfoDto create(ProjectInfo resources) {
        return projectInfoMapper.toDto(projectInfoRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectInfo resources) {
        ProjectInfo projectInfo = projectInfoRepository.findById(resources.getId()).orElseGet(ProjectInfo::new);
        ValidationUtil.isNull( projectInfo.getId(),"ProjectInfo","id",resources.getId());
        projectInfo.copy(resources);
        projectInfoRepository.save(projectInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectInfoDto projectInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", projectInfo.getUserId());
            map.put("标题", projectInfo.getTitle());
            map.put("品牌id", projectInfo.getBrandId());
            map.put("类型id", projectInfo.getCateId());
            map.put("数量", projectInfo.getNumber());
            map.put("年限", projectInfo.getYears());
            map.put("省", projectInfo.getProvince());
            map.put("市", projectInfo.getCity());
            map.put("区", projectInfo.getDistrict());
            map.put("详细信息", projectInfo.getMessage());
            map.put("图片", projectInfo.getImage());
            map.put("阅读次数", projectInfo.getCountNumber());
            map.put("创建时间", projectInfo.getCreateTime());
            map.put("是否删除", projectInfo.getDelFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}